// pages/message/message.js
var p = 1
var appData = getApp().globalData;
var url = appData.apiurl+"member/api/Message/GetList";

var GetList = function (that) {
  that.setData({
    hidden: false
  });
  wx.request({
    url: url,
    data: { UserId: wx.getStorageSync('UserInfo').UserID, CustId: appData.custID, PageIndex: p, PageSize: 5 },
    method:'POST',
    header: {
      "content-type": "application/json"
    },
    success: function (res) {
      wx.hideLoading()
      var l = that.data.LstMessage;
      var strs = that.data.ForMatTimes;
      if(res.statusCode==200&&res.data.code==1200){
        for (var i = 0; i < res.data.content.length; i++) {
          var bool = false;
          l.push(res.data.content[i])
          if(strs.length==0)
          {
            strs.push(res.data.content[i].ForMatTime)
          }else{
             for(var k=0;k<strs.length;k++){
               if (strs[k] == res.data.content[i].ForMatTime){
                  bool=true;
               }
             }
             if(!bool){
               strs.push(res.data.content[i].ForMatTime)
             }
          }
        }
        that.setData({
          LstMessage: l,
          ForMatTimes: strs
        });
        p++;
        that.setData({
          hidden: true
        });
      }
    },
    fail: function (res) {
      wx.hideLoading()
      wx.showModal({ title: '错误', content: '加载错误...', showCancel: false })
    }
  });
}  
Page({
  /**
   * 页面的初始数据
   */
  data: {
     LstMessage:[],
     ForMatTimes:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数  
    var that = this;
    wx.showLoading({ title: '加载中...', mask: true });
    GetList(that)
  },

  /**
   * 清空消息列表信息
   */
  bindClearClickTap:function(event){
     var that=this;
     wx.showModal({
       title: '提示',
       content: '是否清除消息数据列表？',
       success: function (res) {
         if (res.confirm) {
           wx.showLoading({ title: '清除中...', mask: true });
           wx.request({
             url: appData.apiurl+'member/api/Message/Del',
             method:'POST',
             data: { custId: appData.custID,id:1},
             header: {
               "content-type": "application/json"
             },
             success: function (res) {
               if (res.statusCode == 200 && res.data.code == 1200) {
                 wx.hideLoading()
                 that.setData({
                   LstMessage:null
                 })
               } else {
                 wx.hideLoading()
                 wx.showModal({ title: '错误', content: '清除失败', showCancel: false })
               }
             },
             fail: function (res) {
               wx.hideLoading()
               wx.showModal({ title: '错误', content: '清除异常错误...', showCancel: false })
             }
           })
         } else if (res.cancel) {
            console.log("点击取消");
         }
       }
     })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //下拉  
    console.log("下拉");
    p = 1;
    this.setData({
      LstMessage: [],
    });
    var that = this
    GetList(that)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉  
    console.log("上拉")
    var that = this
    GetList(that)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})